// pixtek.AutoScale
// (c) Pixel Technology, August 2001
// info@pixel-technology.com

using System;

namespace pixtek
{
	/// <summary>
	/// Provide world/device scaling support
	/// </summary>
	public class AutoScale
	{
		private int docSize;
		private int viewSize;
		private float scale;

		public AutoScale()
		{
			scale=1;
		}

		/// <summary>
		/// Document size
		/// </summary>
		public int DocSize
		{
			set { docSize=value; }
			get { return docSize; }
		}

		/// <summary>
		/// View size
		/// </summary>
		public int ViewSize
		{
			set { viewSize=value; }
			get { return viewSize; }
		}

		/// <summary>
		/// Scale factor
		/// </summary>
		public float Scale
		{
			set { scale=value; }
			get { return scale; }
		}

		/// <summary>
		/// Give the view range in document unit. 
		/// If zero then view is bigger than document
		/// </summary>
		public int ViewRange
		{
			get
			{
				double RealDocSize = (double)docSize*scale;
				int viewRange = Convert.ToInt32(RealDocSize)-viewSize ;
				if (viewRange<0)
					viewRange=0;
				return viewRange;
			}
		}

		/// <summary>
		/// Convert a view point to a document point
		/// </summary>
		/// <param name="ViewPos">View origin</param>
		/// <param name="Pos">Point in view reference</param>
		/// <returns>Point in document reference</returns>
		public int ViewToDoc (int ViewPos, int Pos)
		{
			float pos = (float)(Pos-ViewPos)/Scale;
			return Convert.ToInt32(pos);
		}

		/// <summary>
		/// Convert a document point to a view point
		/// </summary>
		/// <param name="Pos">In document range</param>
		/// <returns>A position in view range</returns>
		public int DocToView (int Pos)
		{
			float pos = (float)Pos*Scale;
			return Convert.ToInt32(pos);
		}
	}
}
